import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/instrumnet_ledger_list_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/instrument_monitoring_ledger_page/instrument_monitoring_ledger_page_controller.dart';

///仪表监测台账
class InstrumentMonitoringLedgerPagePage extends StatelessWidget {
  InstrumentMonitoringLedgerPagePage({super.key}) {
    Get.lazyPut(InstrumentMonitoringLedgerPageController.new);
  }

  @override
  Widget build(BuildContext context) {
    final InstrumentMonitoringLedgerPageController controller = Get.find<InstrumentMonitoringLedgerPageController>();
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: _getSearchEnterWidget(controller),
                      ),
                      AppGaps.hGap8,
                      _screen(controller),
                      AppGaps.vGap10,
                    ],
                  ),
                  AppGaps.vGap10,
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 10.w),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(8),
                      color: Colors.white,
                    ),
                    child: Column(
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            _topTitle('正常', const Color(0xFF495061), '${controller.data?.normal??''}', const Color(0xFF5C6DFF)),
                            _divider(),
                            _topTitle('高报', const Color(0xFF495061), '${controller.data?.upperLimitCount??''}',const Color(0xFFA5A9C8)),
                            _divider(),
                            _topTitle('高高报', const Color(0xFF495061),'${controller.data?.maxUpperLimitCount??''}', const Color(0xFFFD6B6B)),
                          ],
                        ),
                        AppGaps.vGap10,
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            _topTitle('低报', const Color(0xFF495061), '${controller.data?.lowerLimitCount??''}', const Color(0xFFA5A9C8)),
                            _divider(),
                            _topTitle('低低报', const Color(0xFF495061), '${controller.data?.minLowerLimitCount??''}', const Color(0xFFFD6B6B)),
                          ],
                        ),
                      ],
                    ),
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<InstrumnetLedgerListEntity>(
                    key: controller.globalKey,
                    dataLoad: controller.getDataPage,
                    itemBuilder: (item, index) {
                      return _itemWidget(item, controller);
                    },
                  )),
                ],
              ),
            ), // 右侧筛选栏
            // endDrawer: CustomEndDrawer(
            //   centerContext: Column(
            //     children: [
            //       RegionalOrganizationTreePage(
            //         selectedEntity: controller.selectedEntity,
            //         onCallBack: (RiskTreeEntity entity) {
            //           controller.selectedEntity = entity;
            //         },
            //       ),
            //     ],
            //   ),
            //   reset: controller.reset,
            //   confirm: controller.confirm,
            // ),
          );
        });
  }

  //搜索框
  Widget _getSearchEnterWidget(InstrumentMonitoringLedgerPageController controller) {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    // controller: TextEditingController(text: controller.searchX),
                    onChanged: (value) {
                      controller.searchX = value;
                      controller.update();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '搜索仪表名称',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  ///筛选
  Widget _screen(InstrumentMonitoringLedgerPageController controller) {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }

  Widget _topTitle(String title, Color color, String? value, Color square) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Container(
          width: 10.w,
          height: 10.w,
          color: square,
        ),
        AppGaps.hGap4,
        Text(
          title,
          style: TextStyle(
            fontSize: 14.sp,
            color: color,
            height: 1.2,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.hGap12,
        Text(
          value ?? '空',
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xFF495061),
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  Widget _bottomTitle(String title, Color color, String? value, Color square) {
    return Column(
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 14.sp,
            color: color,
            height: 1.2,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.vGap4,
        Text(
          value ?? '空',
          style: TextStyle(
            fontSize: 13.sp,
            color: const Color(0xFF495061),
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  Widget _getResult() {
    return SvgPicture.asset(
      AppImages.icon_char_graph,
      width: 16.w,
    );
  }

  Widget _rowData(String name, String value, {MainAxisAlignment mainAxisAlignment = MainAxisAlignment.spaceBetween}) {
    return Row(
      mainAxisAlignment: mainAxisAlignment,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Text(
          value,
          style: TextStyle(
            color: const Color(0xFF495061),
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        )
      ],
    );
  }

  Widget _rowDataThree(String name, String value, String state, {MainAxisAlignment mainAxisAlignment = MainAxisAlignment.spaceBetween}) {
    return Row(
      mainAxisAlignment: mainAxisAlignment,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Text(
          value,
          style: TextStyle(
            color: const Color(0xFF495061),
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.hGap6,
        Text(
          state,
          style: TextStyle(
            color: Colors.blue,
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        )
      ],
    );
  }

  ///分割线
  Widget _divider() {
    return Container(
      width: 1.w,
      height: 12.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
        Color(0xFFA0A7B5),
        Color(0xFFA0A7B5),
      ])),
    );
  }

  Widget _itemWidget(InstrumnetLedgerListEntity item, InstrumentMonitoringLedgerPageController controller) {
    return CustomTitleChartPage(
      title: item.taskName ?? '',
      titleRightWidget: _getResult(),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _rowData('${item.objectName ?? '空'}-${item.unitName ?? '空'}-${item.eventName ?? '空'}-${item.controlName ?? '空'}', ''),
          AppGaps.vGap12,
          _rowDataThree('数据采集：', '${item.value}${item.instrumentUnit}', checkStatus('${item.checkStatus}'), mainAxisAlignment: MainAxisAlignment.start),
          AppGaps.vGap12,
          _rowData('上报时间：', item.checkTime ?? ''),
          AppGaps.vGap12,
          Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              color: Colors.white,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _bottomTitle('高报', const Color(0xFF495061), '${item.upperLimit ?? ''}', const Color(0xFF5C6DFF)),
                _divider(),
                _bottomTitle('高高报', const Color(0xFF495061), '${item.maxUpperLimit ?? ''}', const Color(0xFFFD6B6B)),
                _divider(),
                _bottomTitle('低报', const Color(0xFF495061), '${item.lowerLimit ?? ''}', const Color(0xFFA5A9C8)),
                _divider(),
                _bottomTitle('低低报', const Color(0xFF495061), '${item.minLowerLimit ?? ''}', const Color(0xFFFD6B6B)),
              ],
            ),
          ),
        ],
      ),
    ).clickOnce(() {
      controller.clickDetail();
    });
  }

  ///	状态，	0，正常；1，高报；2,高高报；3，低报；4，低低报；
  String checkStatus(String? status) {
    switch (status) {
      case '0':
        return '正常';
      case '1':
        return '高报';
      case '2':
        return '高高报';
      case '3':
        return '低报';
      case '4':
        return '低低报';
      default:
        return '';
    }
  }
}
